﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class TrainTeacherController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.TrainTeacher BllTrainTeacher = new BLL.TrainTeacher();
        [HttpPost]
        [Route("api/TrainTeacher/Add")]
        public IHttpActionResult AddTrainTeacher(Model.TrainTeacher model)
        {
            log.Info($"TeacherID:{model.TeacherID};TeacherName:{model.TeacherName};");

            ResultMsg resultMsg = new ResultMsg();
            //验证教师ID TeacherID是否存在
            if (BllTrainTeacher.Exists(model.TeacherID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "已经存在该教师ID";
                resultMsg.Data = "";
            }
            else
            {
            //int TeacherID= new BLL.SysManage().GetMaxId("TrainTeacher", "TeacherID");
            //model.TeacherID = TeacherID;
                if (BllTrainTeacher.Add(model)>0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = null;
                }
            }

            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/TrainTeacher/Sel")]
        public IHttpActionResult GetTrainTeacherList([FromBody]string DeptID)
        {
            log.Info($"获取教师列表");
            ResultMsg resultMsg = new ResultMsg();
            string swhere = "";
            if (!string.IsNullOrEmpty(DeptID))
            {
                if (DeptID == "0")//
                {
                    DeptID = LoginUser.DepID.ToString();
                }
                swhere = " DeptID=" + DeptID;
            }
            DataTable dt= BllTrainTeacher.GetList(swhere).Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到教师信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        
        

        [HttpPost]
        [Route("api/TrainTeacher/Del")]
        public IHttpActionResult DelTrainTeacher([FromBody]string TeacherID)
        {
            log.Info($"删除教师信息");
            ResultMsg resultMsg = new ResultMsg();
            
            if (BllTrainTeacher.Delete(TeacherID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除教师信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/TrainTeacher/Edit")]
        public IHttpActionResult UpdateTrainTeacher(Model.TrainTeacher model)
        {
            log.Info($"更新教师信息");
            ResultMsg resultMsg = new ResultMsg();
            if (BllTrainTeacher.Update(model))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新教师信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
    }
}
